/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Ui;

import Model.DB;
import Model.TablaImagen;
import Model.Usuario;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.TabableView;

/**
 *
 * @author William
 */
public class ConsultaUsuarios extends javax.swing.JPanel {

    DB db = new DB();
    ArrayList<Usuario> usuarios = db.listarUsuarios();

    public ConsultaUsuarios() {
        initComponents();
        actualizaTabla();
        NuevoProfe.setVisible(false);
        //jTable1= new TablaImagen(columnNames,data);
        this.validate();
        this.repaint();
    }

    void LimpiarJTable(DefaultTableModel miModelo) {
        usuarios = db.listarUsuarios();
        int a = miModelo.getRowCount() - 1;  //Índices van de 0 a n-1
        //System.out.println("Tabla "+a);   //Para mostrar por consola el resultado
        for (int i = a; i >= 0; i--) {

            //System.out.println("i "+i);    //Para mostrar por consola el resultado
            miModelo.removeRow(i);
        }
    }

    private void actualizaTabla() {

        String[] columnNames = new String[]{"Nombre", "Nivel", "Ver", "Modificar", "Eliminar"};
        Object[][] data = new Object[][]{};
        TablaImagen t = new TablaImagen(columnNames, data, jTable1);

        DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
        LimpiarJTable(modelo);
        for (Usuario u : usuarios) {
            //JOptionPane.showMessageDialog(null, u.getNombre());
            modelo.addRow(new Object[]{u.getNombre(), u.getNivelAcceso(), createImage("/Img/eye.png"), createImage("/Img/modi.png"), createImage("/Img/delete.png")});
        }

    }

    public ImageIcon createImage(String path) {
        URL imgURL = getClass().getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelTranslucido1 = new org.edisoncor.gui.panel.PanelTranslucido();
        labelRound2 = new org.edisoncor.gui.label.LabelRound();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        buttonRect1 = new org.edisoncor.gui.button.ButtonRect();
        NuevoProfe = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        textFieldRoundIcon2 = new org.edisoncor.gui.textField.TextFieldRoundIcon();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();

        setOpaque(false);

        labelRound2.setBackground(new java.awt.Color(255, 0, 0));
        labelRound2.setText("Consulta de Usuarios:");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        buttonRect1.setBackground(new java.awt.Color(102, 102, 102));
        buttonRect1.setText("Buscar");
        buttonRect1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRect1ActionPerformed(evt);
            }
        });

        NuevoProfe.setOpaque(false);

        jLabel1.setForeground(new java.awt.Color(204, 204, 204));
        jLabel1.setText("Nombre:");

        jRadioButton1.setText("Administrador");

        jRadioButton2.setText("Estandar");

        javax.swing.GroupLayout NuevoProfeLayout = new javax.swing.GroupLayout(NuevoProfe);
        NuevoProfe.setLayout(NuevoProfeLayout);
        NuevoProfeLayout.setHorizontalGroup(
            NuevoProfeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(NuevoProfeLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(NuevoProfeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(NuevoProfeLayout.createSequentialGroup()
                        .addComponent(jRadioButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jRadioButton2))
                    .addGroup(NuevoProfeLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(textFieldRoundIcon2, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        NuevoProfeLayout.setVerticalGroup(
            NuevoProfeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(NuevoProfeLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(NuevoProfeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(textFieldRoundIcon2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(NuevoProfeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton2))
                .addContainerGap(92, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout panelTranslucido1Layout = new javax.swing.GroupLayout(panelTranslucido1);
        panelTranslucido1.setLayout(panelTranslucido1Layout);
        panelTranslucido1Layout.setHorizontalGroup(
            panelTranslucido1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1)
            .addGroup(panelTranslucido1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelTranslucido1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 605, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelRound2, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(panelTranslucido1Layout.createSequentialGroup()
                        .addComponent(buttonRect1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(NuevoProfe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(169, Short.MAX_VALUE))
        );
        panelTranslucido1Layout.setVerticalGroup(
            panelTranslucido1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTranslucido1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelRound2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(panelTranslucido1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonRect1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(NuevoProfe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(34, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelTranslucido1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panelTranslucido1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonRect1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRect1ActionPerformed
        if (NuevoProfe.isVisible()) {
            NuevoProfe.setVisible(false);
        } else {
            NuevoProfe.setVisible(true);
        }

        this.validate();
        this.repaint();
    }//GEN-LAST:event_buttonRect1ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked

        //JOptionPane.showMessageDialog(null, jTable1.getSelectedColumn()+" "+jTable1.getSelectedRow());
        if (jTable1.getSelectedColumn() == 2) {
 jRadioButton1.setSelected(false);
  jRadioButton2.setSelected(false);
            for (Usuario u : usuarios) {
                //JOptionPane.showMessageDialog(null, u.getNombre());
                if (u.getNombre().equals(jTable1.getValueAt(jTable1.getSelectedRow(), 0))) {
                    textFieldRoundIcon2.setText(u.getNombre());
//                    textFieldRoundIcon1.setText(u.getPrimerApellido() + " " + u.getSegundoApellido());
//                    textFieldRoundIcon3.setText(u.getId());
//                    textFieldRoundIcon5.setText(u.getEmail());
//                    textFieldRoundIcon4.setText(u.getNombreUsuario());
                    if (u.getNivelAcceso().equals("A") || u.getNivelAcceso().equals("a")) {
                        jRadioButton1.setSelected(true);
                    }
                    if (u.getNivelAcceso().equals("E") || u.getNivelAcceso().equals("e")) {
                        jRadioButton2.setSelected(true);
                    }
                    NuevoProfe.setVisible(true);
                }
            }
        }

        if (jTable1.getSelectedColumn() == 4) {
            if (JOptionPane.showConfirmDialog(null, "Desea Eliminar?") == JOptionPane.YES_OPTION) {

                String id = String.valueOf(jTable1.getValueAt(jTable1.getSelectedRow(), 0));
                if (db.borrarUsuario(id)) {
                    JOptionPane.showMessageDialog(null, "Registro borrado");
                    actualizaTabla();
                } else {
                    JOptionPane.showMessageDialog(null, "No se pudo borrar el registro");
                }
                actualizaTabla();

            }
        }
    }//GEN-LAST:event_jTable1MouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel NuevoProfe;
    private org.edisoncor.gui.button.ButtonRect buttonRect1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private org.edisoncor.gui.label.LabelRound labelRound2;
    private org.edisoncor.gui.panel.PanelTranslucido panelTranslucido1;
    private org.edisoncor.gui.textField.TextFieldRoundIcon textFieldRoundIcon2;
    // End of variables declaration//GEN-END:variables
}
